package com.itany.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.nmms.constant.ResponseCodeConstant;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 石小俊
 * @date 2023年11月09日 9:09
 */
@RequestMapping("/type")
public class ProductTypeController {

    private ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeService");

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response) {
        // 获取请求数据
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");

        try {
            PageInfo<ProductType> typePage = typeService.findPage(pageNo, pageSize);
            request.setAttribute("typePage", typePage);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("typeMsg", "服务器内部异常");
        }
        return "backend/productTypeManager";
    }

    // @ResponseBody注解用于Ajax操作
    // 当某个方法被该注解所标注时,表示当前方法不再返回页面
    // 而是将方法的返回值作为数据模型返回,返回给ajax
    // 在ajax中,根据返回值进行指定的操作
    // 即:此时方法的返回值相当于out.print(返回值)
    @ResponseBody
    @RequestMapping("/addType")
    public ResponseResult addType(HttpServletRequest request, HttpServletResponse response) {
        ResponseResult result = new ResponseResult();
        String name = request.getParameter("name");

        try {
            typeService.addType(name);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


    @ResponseBody
    @RequestMapping("/findById")
    public ResponseResult findById(HttpServletRequest request, HttpServletResponse response) {
        ResponseResult result = new ResponseResult();
        String id = request.getParameter("id");
        try {
            ProductType type = typeService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(type);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }


    @ResponseBody
    @RequestMapping("/modifyName")
    public ResponseResult modifyName(HttpServletRequest request, HttpServletResponse response) {
        ResponseResult result = new ResponseResult();
        String id = request.getParameter("id");
        String name = request.getParameter("name");

        try {
            typeService.modifyName(id, name);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }


    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(HttpServletRequest request, HttpServletResponse response) {
        ResponseResult result = new ResponseResult();
        String id = request.getParameter("id");

        try {
            typeService.modifyStatus(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }
}
